package com.meiqiongproperty.yiguanjia.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * author: Administrator
 * date: 2017/5/18.
 * package:com.meiqiongproperty.yiguanjia.ui.dialog
 */

public class PersonalCenterAlertDialog {
    private Context context;
    private Dialog dialog;
    private LinearLayout ll_dialog;
    private TextView dialog_content;
    private Button btn_cancel, btn_confirm;
    private Display display;
    private boolean showPosBtn = false;
    private boolean showNegBtn = false;
    View divide;

    public PersonalCenterAlertDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }


    public PersonalCenterAlertDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.personal_center_dialog_layout, null);
        ll_dialog = (LinearLayout) view.findViewById(R.id.ll_dialog);
        dialog_content = (TextView) view.findViewById(R.id.dialog_content);
        btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        divide = view.findViewById(R.id.divide);

        btn_cancel.setVisibility(View.GONE);
        btn_confirm.setVisibility(View.GONE);
        divide.setVisibility(View.GONE);

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        // 调整dialog背景大小
        ll_dialog.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));
        return this;
    }

    public PersonalCenterAlertDialog setMsg(String msg) {
        if ("".equals(msg)) {
            dialog_content.setText("");
        } else {
            dialog_content.setText(msg);
        }
        return this;
    }

    public PersonalCenterAlertDialog setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public PersonalCenterAlertDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public PersonalCenterAlertDialog setContextTextSize(int size) {
        dialog_content.setTextSize(Utils.sp2px(size));
        return this;
    }

    public PersonalCenterAlertDialog setPositiveButton(String text,
                                                       final View.OnClickListener listener) {
        showPosBtn = true;
        btn_confirm.setVisibility(View.VISIBLE);
        if ("".equals(text)) {
            btn_confirm.setText("确定");
        } else {
            btn_confirm.setText(text);
        }
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
            }
        });
        return this;
    }

    public PersonalCenterAlertDialog setNegativeButton(String text,
                                                       final View.OnClickListener listener) {
        showNegBtn = true;
        btn_cancel.setVisibility(View.VISIBLE);
        if ("".equals(text)) {
            btn_cancel.setText("取消");
        } else {
            btn_cancel.setText(text);
        }
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
            }
        });
        return this;
    }

    public void show() {
        if (showNegBtn && showPosBtn) {
            divide.setVisibility(View.VISIBLE);
        }
        dialog.show();
    }

    public PersonalCenterAlertDialog setButtonTextColor(int color) {
        btn_confirm.setTextColor(color);
        return this;
    }
}
